package com.csii.core.system.aspect;

import com.csii.core.annotation.CostAuth;
import com.csii.core.api.Result;
import com.csii.galaxy.studio.service.GalaxyStudioBaseService;
import lombok.extern.slf4j.Slf4j;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Pointcut;
import org.aspectj.lang.reflect.MethodSignature;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.condition.ConditionalOnBean;
import org.springframework.stereotype.Component;

import java.lang.reflect.Method;

/**
 * @Description: 字典aop类
 * @Author: dangzhenghui
 * @Date: 2019-3-17 21:50
 * @Version: 1.0
 */
@Aspect
@ConditionalOnBean(GalaxyStudioBaseService.class)
@Component
@Slf4j
public class CostAuthAspect {
    @Autowired
    private GalaxyStudioBaseService galaxyStudioBaseService;


    // 定义切点Pointcut
    @Pointcut("@annotation(com.csii.core.annotation.CostAuth)")
    public void excudeService() {
    }

    @Around("excudeService()")
    public Object doAround(ProceedingJoinPoint point) throws Throwable {
        MethodSignature signature = (MethodSignature) point.getSignature();
        Method method = signature.getMethod();
        CostAuth pd = method.getAnnotation(CostAuth.class);
        Result result= galaxyStudioBaseService.costAuth(pd.value());
        if(!result.isSuccess())
            return result;
        return point.proceed();
    }


}
